package frame.options.simulation.results.virtual.graphical;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.components.links.link.Link;
import model.components.networks.virtual.VirtualNetwork;
import model.components.nodes.node.Node;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import frame.options.simulation.results.ResultsFrame;

@SuppressWarnings("serial")
public class VirtualNetworkViewersPanel extends JPanel implements ChangeListener
{
	private List< VirtualNetwork > networksRequest;
	public List< VirtualNetwork > getVirtualNetworks(){ return networksRequest; }
	public void setNetworksRequest( List< VirtualNetwork > networksRequest ){ this.networksRequest = networksRequest; }
	
	private ResultsFrame frameResults;
	public ResultsFrame getFrameResults(){ return frameResults; }
	public void setFrameResults( ResultsFrame frameResults ){ this.frameResults = frameResults; }
		
	private List< VirtualNetworkViewer > visualizationViewersNetworkRequest;
	public List< VirtualNetworkViewer > getVisualizationViewersNetworkRequest(){ return visualizationViewersNetworkRequest; }
	public void setVisualizationViewersNetworkRequest( List< VirtualNetworkViewer > visualizationViewersNetworkRequest ){ this.visualizationViewersNetworkRequest = visualizationViewersNetworkRequest; }
	
	private JLabel label;
	public JLabel getLabel(){ return label; }
	public void setLabel( JLabel label ){ this.label = label; }
	
	private JSlider slider;
	public JSlider getSlider(){ return slider; }
	public void setSlider( JSlider slider ){ this.slider = slider; }
	
	private int positionPrevious;
	public int getPositionPrevious(){ return positionPrevious; }
	public void setPositionPrevious( int positionPrevious ){ this.positionPrevious = positionPrevious; }
	
	public VirtualNetworkViewersPanel( List< VirtualNetwork > virtualNetworks, ResultsFrame frameResults )
	{
		this.setNetworksRequest( virtualNetworks );
		this.setFrameResults( frameResults );
		
		this.setVisualizationViewersNetworkRequest( this.visualizationViewersNetworkRequest( virtualNetworks ) );
		
		this.setLabel( new JLabel( virtualNetworks.get( 0 ).getName(), null, SwingConstants.CENTER ) );
		this.setSlider( this.slider( virtualNetworks.size() ) );
		this.setLayout( new BorderLayout() );
		this.add( this.getLabel(), BorderLayout.NORTH );
		this.add( this.getVisualizationViewersNetworkRequest().get( 0 ), BorderLayout.CENTER );
		this.add( this.getSlider(), BorderLayout.SOUTH );
	}
	
	private List< VirtualNetworkViewer > visualizationViewersNetworkRequest( List< VirtualNetwork > virtualNetworks )
	{
		List< VirtualNetworkViewer > visualizationViewersNetworkRequest = new ArrayList< VirtualNetworkViewer >();
		
		for( VirtualNetwork virtualNetwork: virtualNetworks )
		{
			CircleLayout< Node, Link > circleLayout = new CircleLayout< Node, Link >( virtualNetwork.getGraph() );
			circleLayout.setGraph( virtualNetwork.getGraph() );
			circleLayout.setVertexOrder( virtualNetwork.getNodes() );
			
			VirtualNetworkViewer visualizationViewerNetworkRequest = new VirtualNetworkViewer( circleLayout, this.getFrameResults() );
			visualizationViewerNetworkRequest.setBackground( Color.GRAY );
			
			visualizationViewersNetworkRequest.add( visualizationViewerNetworkRequest );
		}
		
		return visualizationViewersNetworkRequest;
	}
	
	private JSlider slider( int numberNetworksRequest )
	{
		JSlider slider = new JSlider();
		slider.setPaintTicks( true );
		slider.setSnapToTicks( true );
		slider.setPaintTrack( true );
		slider.setPaintLabels( true );
		slider.setMinimum( 0 );
		slider.setMaximum( numberNetworksRequest - 1 );
		slider.setMinorTickSpacing( 1 );
		slider.setMajorTickSpacing(	1 );
		slider.setValue( 0 );
		slider.addChangeListener( this );
		
		return slider;
	}
	
	@Override
	public void stateChanged( ChangeEvent changeEvent )
	{
		this.putValueSliders( this.getSlider().getValue() );
	}
	
	public void putValueSliderss( int i )
	{
		this.putValueSliders( i );
		
		this.getFrameResults().getVirtualNetworksTabbedPane().setSelectedIndex( 0 );
	}
	
	public void putValueSliders( int i )
	{
		this.putValueSlider( i );
		
		int selectedIndex = this.getFrameResults().getVirtualNetworksViewersPanelsTabbedPane().getSelectedIndex();
		this.getFrameResults().getVirtualNetworksTablesPanelsTabbedPane().setSelectedIndex( selectedIndex );
		this.getFrameResults().getVirtualNetworksTablesPanels().get( selectedIndex ).putValueSlider( i );
	}
	
	public void putValueSlider( int i )
	{
		VirtualNetwork virtualNetwork = this.getVirtualNetworks().get( i );
		
		if( virtualNetwork.getTimeStamp() == -1 )
			this.getLabel().setText( virtualNetwork.getName() );
		else
			this.getLabel().setText( "Time: " + virtualNetwork.getTimeStamp() );
		
		this.remove( this.getVisualizationViewersNetworkRequest().get( this.getPositionPrevious() ) );
		this.setPositionPrevious( i );
		this.add( this.getVisualizationViewersNetworkRequest().get( i ), BorderLayout.CENTER );
		this.getSlider().setValue( i );
		this.repaint();
	}

}
